{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Numpy\n",
    "**Numpy是一个专门用来做科学计算的一个包，数据存储结构是纯数组，也就是没有索引跟列名的，所以运算速度快!**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、 创建数组\n",
    "**数组创建 数组属性**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**创建一维数组 array([])**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([0,1,2,3,4,5,6,7,8])\n",
    "print(arr1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、 数组属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组维度: (9,)\n",
      "数组轴个数: 1\n",
      "元素总个数: 9\n",
      "int32\n"
     ]
    }
   ],
   "source": [
    "# 数组的维度 m*n\n",
    "print('数组维度:',arr1.shape)\n",
    "# 数组轴的个数\n",
    "print('数组轴个数:',arr1.ndim)\n",
    "# 数组元素的总个数\n",
    "print('元素总个数:',arr1.size)\n",
    "# 描述数组中元素类型的对象\n",
    "print(arr1.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**创建二维数组**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n"
     ]
    }
   ],
   "source": [
    "#二维数组\n",
    "arr2 = np.arange(9).reshape(3,3)# 3行3列\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组维度: (3, 3)\n",
      "数组轴个数: 2\n",
      "元素总个数: 9\n"
     ]
    }
   ],
   "source": [
    "# 数组的维度 m*n\n",
    "print('数组维度:',arr2.shape)\n",
    "# 数组轴的个数\n",
    "print('数组轴个数:',arr2.ndim)\n",
    "# 数组元素的总个数\n",
    "print('元素总个数:',arr2.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2]\n",
      "  [ 3  4  5]\n",
      "  [ 6  7  8]]\n",
      "\n",
      " [[ 9 10 11]\n",
      "  [12 13 14]\n",
      "  [15 16 17]]\n",
      "\n",
      " [[18 19 20]\n",
      "  [21 22 23]\n",
      "  [24 25 26]]]\n"
     ]
    }
   ],
   "source": [
    "#三维数组\n",
    "arr3 = np.arange(27).reshape(3, 3, 3)\n",
    "print(arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组维度: (3, 3, 3)\n",
      "数组轴个数: 3\n",
      "元素总个数: 27\n"
     ]
    }
   ],
   "source": [
    "# 数组的维度 m*n\n",
    "print('数组维度:',arr3.shape)\n",
    "# 数组轴的个数\n",
    "print('数组轴个数:',arr3.ndim)\n",
    "# 数组元素的总个数\n",
    "print('元素总个数:',arr3.size)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**创建数值全为0或1的数组**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0. 0. 0.]\n",
      "  [0. 0. 0.]\n",
      "  [0. 0. 0.]]\n",
      "\n",
      " [[0. 0. 0.]\n",
      "  [0. 0. 0.]\n",
      "  [0. 0. 0.]]\n",
      "\n",
      " [[0. 0. 0.]\n",
      "  [0. 0. 0.]\n",
      "  [0. 0. 0.]]]\n"
     ]
    }
   ],
   "source": [
    "print(np.zeros((3,3,3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1. 1. 1.]\n",
      "  [1. 1. 1.]\n",
      "  [1. 1. 1.]]\n",
      "\n",
      " [[1. 1. 1.]\n",
      "  [1. 1. 1.]\n",
      "  [1. 1. 1.]]\n",
      "\n",
      " [[1. 1. 1.]\n",
      "  [1. 1. 1.]\n",
      "  [1. 1. 1.]]]\n"
     ]
    }
   ],
   "source": [
    "print(np.ones((3,3,3)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**描述数组中元素类型 array.dtype**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int32\n"
     ]
    }
   ],
   "source": [
    "# 描述数组中元素类型的对象\n",
    "print(arr1.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**数组元素类型转换**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3. 4. 5.]\n",
      "float64\n",
      "[1.+0.j 2.+0.j 3.+0.j 4.+0.j 5.+0.j]\n",
      "complex128\n"
     ]
    }
   ],
   "source": [
    "# 转换数组中元素的类型\n",
    "arr4 = np.array([1,2,3,4,5], dtype='float64')\n",
    "arr5 = np.array([1,2,3,4,5], dtype='complex')\n",
    "print(arr4)\n",
    "print(arr4.dtype)\n",
    "print(arr5)\n",
    "print(arr5.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、 基本运算\n",
    "**数组运算 常用函数**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**数组运算**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "arr6 = np.array([20,30,40,50])\n",
    "arr7 = np.arange(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[20 29 38 47]\n"
     ]
    }
   ],
   "source": [
    "print(arr6-arr7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 40  60  80 100]\n"
     ]
    }
   ],
   "source": [
    "print(arr6*2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**常用函数**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 返回输入数量的等比间隔，linspace(start, stop, num=50)\n",
    "arr8 = np.linspace(1,4,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4.])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.0\n"
     ]
    }
   ],
   "source": [
    "# 求和\n",
    "print(arr8.sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.  3.  6. 10.]\n"
     ]
    }
   ],
   "source": [
    "# 累加\n",
    "print(arr8.cumsum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.         0.69314718 1.09861229 1.38629436]\n"
     ]
    }
   ],
   "source": [
    "# 对数\n",
    "print(np.log(arr8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2.71828183  7.3890561  20.08553692 54.59815003]\n"
     ]
    }
   ],
   "source": [
    "# 指数\n",
    "print(np.exp(arr8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.         1.41421356 1.73205081 2.        ]\n"
     ]
    }
   ],
   "source": [
    "# 开方\n",
    "print(np.sqrt(arr8))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、 索引，切片和迭代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n"
     ]
    }
   ],
   "source": [
    "print(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 4 7]\n"
     ]
    }
   ],
   "source": [
    "print(arr2[:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4 5]\n"
     ]
    }
   ],
   "source": [
    "print(arr2[1, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 5 6 7 8]\n"
     ]
    }
   ],
   "source": [
    "print(arr2[arr2>3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2]\n",
      "  [ 3  4  5]\n",
      "  [ 6  7  8]]\n",
      "\n",
      " [[ 9 10 11]\n",
      "  [12 13 14]\n",
      "  [15 16 17]]\n",
      "\n",
      " [[18 19 20]\n",
      "  [21 22 23]\n",
      "  [24 25 26]]]\n"
     ]
    }
   ],
   "source": [
    "print(arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[20 23 26]\n"
     ]
    }
   ],
   "source": [
    "print(arr3[-1][:,-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1]\n",
      "  [ 3  4]]\n",
      "\n",
      " [[ 9 10]\n",
      "  [12 13]]\n",
      "\n",
      " [[18 19]\n",
      "  [21 22]]]\n"
     ]
    }
   ],
   "source": [
    "print(arr3[0:,0:2,0:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 迭代\n",
    "for row in arr3[0][0]:\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n",
      "18\n",
      "19\n",
      "20\n",
      "21\n",
      "22\n",
      "23\n",
      "24\n",
      "25\n",
      "26\n"
     ]
    }
   ],
   "source": [
    "# flat数组元素迭代器\n",
    "for element in arr3.flat:\n",
    "    print(element)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## #课堂练习题\n",
    "**创建一个10*10的数组，并且边框是1，里面是0**"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "iVBORw0KGgoAAAANSUhEUgAAAVEAAADeCAYAAACE5zP7AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAA4qSURBVHhe7d2/i1ZXGgfws1tYiMSAhSLEjAQEi4VNCk0a0SKdMEMC+dGY9R8IkkamkqlCmpD/wI1NNoEsCnYWSppEi00ghZBG14DEIpAEkcVm95773vs6s4nj3DnvOWfe934+YIJXyGPe595n7rlz53z/9Og/j/8bANiWP3f/BmAbDFGABIYoQAJDFCCBIQqQ4OlD9Od/hHc/3RV2d78++qE73vvhw+mf7f70rfD5z91xgLnyTfhos1n3DM+4E10L1997HB41v84f6Q6tt+dSuNP++Rfh7X3dsS16cOutbf2FZ6Fm7ZvXan3R6U6UL/8RHnRHium/IF/7pjtQjl6XNo/n2avhfDvH7oWLe7pDA1RYzv8UPv9yVzgX3ggXuiPl1Kw9Oblu7L0UlrsjxbSrhuth6ehKd6CcdojdCGHlYHegGL0ubpTnWYUh+uDW++HuXx6Hz44tdUfKqVn75rWPw9Lp5o7+pe5AMc0F/f3hZsWwGk52R4pp7gzO/fpBePTmO6H0J67XpY3zPIuKD9H9x77440cDBdSsffz14Y88ZqNZqjQn1/7ud0Xteyd89vqr3W/K0uvSxnmeRRWW8wCLwxAFSGCIAiTYoUN08p3VKq+H9K9J1HhFo3/3tsKrQDVrT14FqvEakvOstHq9zle7/BDtG/jpibDW/Hbt64IfasXa/fuKu6+eCVfC5XD2aqxd4uLtB8WucPj25RAengmH49+jyMU7edUn1j51v/nt/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QnOU8QAJDFCCBIQqQwBAFSGCIAiQwRAESGKIACQxRgATbH6LTjUT8xBIwzzaGKQ7dDCntTlTa5yDSPqV95lez1zVrN/rtBQfXlfa5dfFDlgAp7TO3Ufa6bm1pn4VIgIykfeY10l5Xri3tswgJkK32LknaZz7SPlsla0v7LE8CZOEUxuaCis/IpH2OoNc1a1cyuiEqAXJV2md20j6lfS4kCZDTfKFCg0zaZ3doBL2W9rnj9Euh2b6WIu0zaj5baZ+dHOeZtM/Nakv7nIW+gdI+pX3mpNeFey3tc7g4DKV9AgsjrkykfQIUlTZEpX0Cc69/HCDtE6A4y3mABIYoQAJDFCCBIQqQwBAFSGCIAiQwRAESGKIACbY/RKcbifiJJWCebQxT/N1mSP2OW0+ZdWl3ovOY9tmoWXuyHVeNLzrdiVIoMG2D/iQstKvOenpd2jyeZ89I+4zxI+2fx536f6/Ccn6yh2OVtM+qtScn1429l8Jyd6SYdtVwPSwdXekOlFMvhVGvixvleVZhiNZM+6yaNHrt47B0+nE4/1J3oJjmgm7jOVbDye5IMc2dQa0URr0ubZznWVR8iNZL+6xb+/jrwx95zEazVKm152vFFEa9Lm2c51lUYTkPsDgMUYAEhihAgh06RCffWa3yekj/mkSNVzT6d28rvApUs3a9BEjnWWnSPmehb2CNtM+Ktfv3FXdfPROuhMtdEmSJi7cfFLvC4duXn0S6FLl4J6/6xNrFUxj1unCvR3qeNaR9ArTiF4LN0j7jsI6vr218+8IzUYAEaUNU2icw9/rHAU9J++yfX3ePhv6ftE+ABJbzAAkMUYAEhihAAkMUYNtC+B8blUEwWpebRAAAAABJRU5ErkJggg=="
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5、 更改数组的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8]\n"
     ]
    }
   ],
   "source": [
    "# 将多维数组降成一维\n",
    "print(arr2.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a1 = np.array([9,10,11])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2]\n",
      " [ 3  4  5]\n",
      " [ 6  7  8]\n",
      " [ 9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "# 横向添加数组\n",
    "a2 = np.vstack((arr2,a1))\n",
    "print(a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "# 不改变原有数组\n",
    "print(a2.reshape(2,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a3 = a2.reshape(2,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 改变原有数组\n",
    "a2.resize(2,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "print(a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  6]\n",
      " [ 1  7]\n",
      " [ 2  8]\n",
      " [ 3  9]\n",
      " [ 4 10]\n",
      " [ 5 11]]\n"
     ]
    }
   ],
   "source": [
    "# 转变形状\n",
    "print(a2.transpose())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "print(a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[0, 1],\n",
      "       [6, 7]]), array([[2, 3],\n",
      "       [8, 9]]), array([[ 4,  5],\n",
      "       [10, 11]])]\n"
     ]
    }
   ],
   "source": [
    "# 垂直切分\n",
    "print(np.hsplit(a2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0. 0. 0. 0.]\n",
      " [0. 1. 0. 0. 0.]\n",
      " [0. 0. 1. 0. 0.]\n",
      " [0. 0. 0. 1. 0.]\n",
      " [0. 0. 0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "# 生成对角矩阵\n",
    "print(np.eye(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6、 矩阵计算基础\n",
    "**矩阵相乘**\n",
    "\n",
    "矩阵A和B必须是相符的矩阵，A的列要等于B的行。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 8 10  4]\n",
      " [26 31 13]\n",
      " [44 52 22]]\n"
     ]
    }
   ],
   "source": [
    "A = np.array([\n",
    "[1, 2],\n",
    "[4, 5],\n",
    "[7, 8]\n",
    "])\n",
    "B = np.array([\n",
    "[4, 4, 2],\n",
    "[2, 3, 1],\n",
    "])\n",
    "print(np.dot(A, B))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 逆矩阵\n",
    "矩阵与逆矩阵相乘为 [[1,0][0,1]]\n",
    "\n",
    "## $$A^{-1}*A=In$$\n",
    "## $$A*A^{−1}=In$$\n",
    "## $$In=[[1.,0.][0.,1.]]$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  1  2]\n",
      " [ 1 -2 -1]\n",
      " [-1 -2  1]]\n",
      "[[ 0.25    0.3125 -0.1875]\n",
      " [-0.     -0.25   -0.25  ]\n",
      " [ 0.25   -0.1875  0.3125]]\n",
      "[[ 1.00000000e+00 -5.55111512e-17  0.00000000e+00]\n",
      " [ 0.00000000e+00  1.00000000e+00  0.00000000e+00]\n",
      " [ 0.00000000e+00 -2.77555756e-17  1.00000000e+00]]\n"
     ]
    }
   ],
   "source": [
    "A=np.array([[2,1,2],[1,-2,-1],[-1,-2,1]])\n",
    "# 计算逆矩阵\n",
    "A_=np.linalg.inv(A)\n",
    "print(A)\n",
    "print(A_)\n",
    "print(np.dot(A,A_))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
